package com.tg.aop;

import com.tg.controller.Code;
import com.tg.controller.ResultMessage;
import com.tg.exception.BusinessException;
import com.tg.exception.SystemException;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class ExceptionAdvice {

    private Exception e;

    @Pointcut("execution(* com.tg.mapper.*Mapper.*(..))")
    public void daoExceptionPt() {
    }

    @Pointcut("execution(* com.tg.service.*Service.*(..))")
    public void serviceExceptionPt() {
    }


    @AfterThrowing(value = "daoExceptionPt()", throwing = "e")
    public void daoException(Exception e) {
        this.e = e;
        e.printStackTrace();
        throw new SystemException(Code.SYSTEM_ERR, ResultMessage.SYSTEM_ERR, e);
    }

    @AfterThrowing(value = "serviceExceptionPt()", throwing = "e")
    public void serviceException(Exception e) {
        if (this.e == null){
            e.printStackTrace();
            throw new BusinessException(Code.BUSINESS_ERR, ResultMessage.BUSINESS_ERR, e);
        }
    }
}
